package Form;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

public class ModifyForm extends JDialog {
	DecimalFormat pf = new DecimalFormat("##.###%");
	JList<String> list;
	DefaultListModel<String> listModel = new DefaultListModel<String>();
	JLabel lblRuleInfo = new JLabel("New label");
	MainForm mainForm;
	/**
	 * Create the dialog.
	 */
	public ModifyForm(MainForm main) {
		mainForm = main;
		String[] rules = main.getString(main.classifier.getOrTree());
		for (int i = 0; i < rules.length; i++) {
			listModel.addElement(rules[i]);
		}
		
		
		setBounds(100, 100, 725, 566);
		BorderLayout borderLayout = new BorderLayout();
		borderLayout.setVgap(5);
		borderLayout.setHgap(5);
		getContentPane().setLayout(borderLayout);
		{	
			list = new JList<String>(listModel);
			list.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent arg0) {
					int i = list.getSelectedIndex();
					if(i!=-1)
					{
						String text = "rule no " + String.valueOf(i) + " : ";
						double supp = (double) (mainForm.classifier.getOrTree().getRuleInfoAt(i).suppCount)
						/ mainForm.classifier.getNoInstances();
						double conf = mainForm.classifier.getOrTree().getRuleInfoAt(i).conf;
						text += " supp: " + pf.format(supp) + "   ,conf: " + pf.format(conf) ;
						lblRuleInfo.setText(text);
					}
				}
			});
			list.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					
				}
			});
			
			JScrollPane scrollPane = new JScrollPane(list);
			getContentPane().add(scrollPane, BorderLayout.CENTER);
			
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
		}
		{
			JPanel panel = new JPanel();
			FlowLayout flowLayout = (FlowLayout) panel.getLayout();
			flowLayout.setAlignment(FlowLayout.RIGHT);
			getContentPane().add(panel, BorderLayout.NORTH);
			{
				panel.add(lblRuleInfo);
				lblRuleInfo.setFont(new Font("Tahoma", Font.PLAIN, 14));
			}
			{
				JButton btnDelete = new JButton("Delete");
				panel.add(btnDelete);
				btnDelete.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseReleased(MouseEvent arg0) {
						int i = list.getSelectedIndex();
						if(i!=-1)
						{
							mainForm.classifier.getOrTree().removeRuleAt(i);
							
							listModel.remove(i);
						}
					}
				});
			}
		}
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.EAST);
		}
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.WEST);
		}
	}

}
